package com.example.haosiji.app;

import java.io.File;
import java.nio.channels.ClosedByInterruptException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.provider.CallLog;
import android.text.TextUtils;
import android.widget.SimpleAdapter;

public class DBManager {
	
	public static ArrayList<Map<String, Object>> mList;
	
	
	private static void createTable(SQLiteDatabase paramSQLiteDatabase,String paramString1, String paramString2){
		
		try{
			paramSQLiteDatabase.rawQuery("SELECT * FROM"+paramString1,null);
			return;
			
		}catch(Exception loException){
			paramSQLiteDatabase.execSQL(paramString2);
		}
		
	}
	
	private static void closeDB(SQLiteDatabase paramSQLiteDatabase){
	    if ((paramSQLiteDatabase != null) && (paramSQLiteDatabase.isOpen()))
	      paramSQLiteDatabase.close();
	  }
	
	/*创建表*/
	private static void createTables(){
		SQLiteDatabase localSQLiteDatabase = null;
		localSQLiteDatabase = openDatabase();
		if(localSQLiteDatabase != null){
			createTable(localSQLiteDatabase,"driver_list","CREATE TABLE IF NOT EXISTS driver_list( _id INTEGER PRIMARY KEY AUTOINCREMENT ,name TEXT NOT NULL,  phone TEXT NOT NULL , DateTime long NOT NULL  )");
		}
		closeDB(localSQLiteDatabase);
	}
	
	private static SQLiteDatabase openDatabase(){
		return SQLiteDatabase.openOrCreateDatabase(AppInfo.BASE_DATA_DIR+File.separator+"haosiji.db",null);
	}
	
	/*初始化数据库*/
	public static void initDataBase(Context paramContext){
		createTables();
	}
	
	/*往数据库插入数据*/
	public static void saveDriverInfo(String paramString1, String paramString2, long dateTime){
		
		SQLiteDatabase localSQLiteDatabase = null;
		SQLiteStatement localSQLiteStatement = null;
		localSQLiteDatabase = openDatabase();
		if(localSQLiteDatabase != null ){
			localSQLiteStatement = localSQLiteDatabase.compileStatement("INSERT INTO driver_list (name, phone, DateTime) VALUES( ?, ?, ?)");
			setBindTextValue(localSQLiteStatement, 1, paramString1);
	        setBindTextValue(localSQLiteStatement, 2, paramString2);
	        localSQLiteStatement.bindLong(3, dateTime);
	        
	        localSQLiteStatement.execute();
		}
		closeDB(localSQLiteDatabase);
	}
	
	
	public static void deleteMyDriverById(String paramString)throws Exception{
		SQLiteDatabase localSQLiteDatabase = openDatabase();
		if (localSQLiteDatabase != null){
			localSQLiteDatabase.execSQL("DELETE FROM driver_list WHERE _id = '" + paramString + "'");
		}
		      
		closeDB(localSQLiteDatabase);
	}
	
	public static void deleteAllMyDriver()throws Exception{
		SQLiteDatabase localSQLiteDatabase = openDatabase();
		if (localSQLiteDatabase != null){
			localSQLiteDatabase.execSQL("DELETE FROM driver_list");
		}
		      
		closeDB(localSQLiteDatabase);
	}
	
	
	
	public static ArrayList<Map<String, Object>> getDriverInfo(){
		
		mList = new ArrayList<Map<String,Object>>(); 
		
		SQLiteDatabase localSQLiteDatabase = null;
		Cursor localCursor = null;
		
		try{
			
			localSQLiteDatabase = openDatabase();
			
			if(localSQLiteDatabase != null){
					localCursor = localSQLiteDatabase.rawQuery("SELECT * FROM driver_list order BY DateTime DESC", null);
					
					for(localCursor.moveToFirst();!localCursor.isAfterLast();localCursor.moveToNext()){
						Map<String,Object> item = new HashMap<String,Object>(); 
		            	
		            	String oder_id = localCursor.getString(localCursor
								.getColumnIndex("_id"));
		           	 	item.put("order_id", oder_id);
		            	
		           	 	Date date = new Date(localCursor.getLong(localCursor
									.getColumnIndex("DateTime")));
		           	 	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		           	 	String dateTime = df.format(date);
		           	 	item.put("date", dateTime);
		           	 	
		           	 	String DriverPhone = localCursor.getString(localCursor
								.getColumnIndex("phone"));
		           	 	item.put("phone", DriverPhone);
		           	 
		           	 	String Drivername = localCursor.getString(localCursor
							.getColumnIndex("name"));
		           	 	item.put("name", Drivername);
		           	
		           	 	mList.add(item);
					}
					
					return mList;
					
//					if(localCursor.moveToFirst()){
//						
//						while (localCursor.moveToNext()) { 
//			            	Map<String,Object> item = new HashMap<String,Object>(); 
//			            	
//			            	String oder_id = localCursor.getString(localCursor
//									.getColumnIndex("_id"));
//			           	 	item.put("order_id", oder_id);
//			            	
//			           	 	Date date = new Date(localCursor.getLong(localCursor
//										.getColumnIndex("DateTime")));
//			           	 	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			           	 	String dateTime = df.format(date);
//			           	 	item.put("date", dateTime);
//			           	 	
//			           	 	String DriverPhone = localCursor.getString(localCursor
//									.getColumnIndex("phone"));
//			           	 	item.put("phone", DriverPhone);
//			           	 
//			           	 	String Drivername = localCursor.getString(localCursor
//								.getColumnIndex("name"));
//			           	 	item.put("name", Drivername);
//			           	
//			           	 	mList.add(item);
//			           	 	
//			            }  
//						
//						return mList;
//					}
					
				}
			
		}finally{
			 if (localCursor != null) {  
				 localCursor.close();  
             } 
			 
			 closeDB(localSQLiteDatabase);
		}
		
		return mList;
	}
	
	
	private static void setBindTextValue(SQLiteStatement paramSQLiteStatement, int paramInt, String paramString){
	    if (TextUtils.isEmpty(paramString))
	    {
	      paramSQLiteStatement.bindNull(paramInt);
	      return;
	    }
	    paramSQLiteStatement.bindString(paramInt, paramString);
	  }
	
	
	
}
